package com.achen.direct;

import com.achen.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author achen
 * @create 2020-05-10 23:07
 */
public class Consumer1 {
    public static void main(String[] args) throws IOException {
        //获取连接
        Connection connection = RabbitMQUtil.getConnection();
        //获取通道
        Channel channel = connection.createChannel();
        //交换机名称
        String exchangeName = "logs_direct";
        //通过通道声明交换机以及交换机的类型
        channel.exchangeDeclare(exchangeName,"direct");
        //创建一个临时队列
        String queue = channel.queueDeclare().getQueue();
        //基于routingKey绑定队列和交换机
        channel.queueBind(queue,exchangeName,"error");
        //消费消息
        channel.basicConsume(queue,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者1 ---> "+new String(body));
            }
        });

    }
}
